﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Icinetic.FeedCategorySample.Entities;
using Windows.UI.Notifications;

namespace Icinetic.FeedCategorySample.Common.Tiles
{
    public class TilesHelper
    {
        public void Publish(Dictionary<string,List<RssItem>> items, string logoImage)
        {
            int unreadCount = 0;
            List<RssItem> itemsToTiles = new List<RssItem>();
            foreach (KeyValuePair<string, List<RssItem>> rss in items)
            {
                unreadCount += rss.Value.Where(x => !x.IsRead && x.TitleAll.Length > 0).Count();
                itemsToTiles.Add(rss.Value.Where(x => !x.IsRead && x.TitleAll.Length > 0).FirstOrDefault());
            }

            TileUpdater updater = Windows.UI.Notifications.TileUpdateManager.CreateTileUpdaterForApplication();
            if (updater != null && updater.Setting == NotificationSetting.Enabled)
            {
                updater.EnableNotificationQueue(true);

                PublishImageTile(logoImage, unreadCount);
                foreach (RssItem item in itemsToTiles.Take(4))
                    if (item != null)
                        PublishImageWithTitleTile(item);
            }
        }

        private static void PublishImageWithTitleTile(RssItem item)
        {
            //Wide
            var tileImageTitle = Windows.UI.Notifications.TileUpdateManager.GetTemplateContent(Windows.UI.Notifications.TileTemplateType.TileWideImageAndText01);
            var tileTextAttributes3 = tileImageTitle.GetElementsByTagName("text");
            tileTextAttributes3[0].AppendChild(tileImageTitle.CreateTextNode(item.TitleAll));
            var tileImageAttributes3 = tileImageTitle.GetElementsByTagName("image");
            if (string.IsNullOrEmpty(item.Image))
                tileImageAttributes3[0].Attributes[1].NodeValue = "Assets/noimage-hor.png";
            else
                tileImageAttributes3[0].Attributes[1].NodeValue = item.Image;

            //Square
            var tileImageSquare = Windows.UI.Notifications.TileUpdateManager.GetTemplateContent(Windows.UI.Notifications.TileTemplateType.TileSquareImage);
            var tileImageSquareImage = tileImageSquare.GetElementsByTagName("image");
            if (string.IsNullOrEmpty(item.Image))
                tileImageSquareImage[0].Attributes[1].NodeValue = "Assets/noimage-hor.png";
            else
                tileImageSquareImage[0].Attributes[1].NodeValue = item.Image;

            var node = tileImageTitle.ImportNode(tileImageSquare.GetElementsByTagName("binding").Item(0), true);
            tileImageTitle.GetElementsByTagName("visual").Item(0).AppendChild(node);

            var tileImageTitleNotification = new Windows.UI.Notifications.TileNotification(tileImageTitle);
            Windows.UI.Notifications.TileUpdateManager.CreateTileUpdaterForApplication().Update(tileImageTitleNotification);
        }

        private static void PublishImageTile(string logo, int totalNews)
        {
            //Wide
            var tile = Windows.UI.Notifications.TileUpdateManager.GetTemplateContent(Windows.UI.Notifications.TileTemplateType.TileWideImageAndText01);
            var tileText = tile.GetElementsByTagName("text");
            tileText[0].AppendChild(tile.CreateTextNode(totalNews + " noticias no leídas."));
            var tileImageAttributes3 = tile.GetElementsByTagName("image");
            tileImageAttributes3[0].Attributes[1].NodeValue = logo;

            //Square
            var tileSquare = Windows.UI.Notifications.TileUpdateManager.GetTemplateContent(Windows.UI.Notifications.TileTemplateType.TileSquareText04);
            var tileTextSquare = tileSquare.GetElementsByTagName("text");
            tileTextSquare[0].AppendChild(tileSquare.CreateTextNode(totalNews + " noticias no leídas."));

            var node = tile.ImportNode(tileSquare.GetElementsByTagName("binding").Item(0), true);
            tile.GetElementsByTagName("visual").Item(0).AppendChild(node);

            var tileLogoNotification = new Windows.UI.Notifications.TileNotification(tile);
            Windows.UI.Notifications.TileUpdateManager.CreateTileUpdaterForApplication().Update(tileLogoNotification);
        }
    }
}
